import re

from django.contrib.auth.backends import ModelBackend

from users.models import User


class UsernameMobileAuthBackend(ModelBackend):
    def authenticate(self, request, mobile=None, password=None, **kwargs):
        try:
            # print(mobile,password)
            if re.match(r'^1[3-9]\d{9}$', mobile):
                user=User.objects.get(mobile=mobile)
            else:
                return None

        except:
            return None

        # print('user',user)
        if user.check_password(password):
            return user
        else:
            return None

